package com.icebartech.base.common.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.icebartech.base.modules.common.dto.DictDTO;
import com.icebartech.base.modules.common.facade.SysCodeFacade;
import com.icebartech.core.utils.DateTimeUtility;
import com.icebartech.core.vo.ResponseData;


/**
 * 把SysCode字典里面的内容加载到内存，方便程序中快速查找使用
 * @author Administrator
 */
@Component
public class LoadSysCodeUtil{
	private static Logger logger = LoggerFactory.getLogger(LoadSysCodeUtil.class);
	
	/**国家字典**/
	public static Map<String, String> countryMap = new HashMap<>();
	/**省份字典**/
	public static Map<String, String> provinceMap = new HashMap<>();
	/**城市字典**/
	public static Map<String, String> cityMap = new HashMap<>();
	/**地区字典**/
	public static Map<String, String> districtMap = new HashMap<>();
	/**
	 * 银行列表字典
	 */
	public static Map<String, String> bankMap = new HashMap<>();
	
	@Autowired
	private SysCodeFacade sysCodeFacade;
	
	@PostConstruct
	public void run() throws Exception {
		logger.info("init sysCode into memory start at time:{}", DateTimeUtility.timeofnow());
		
		try{
			//加载国家字典
			ResponseData<List<DictDTO>> countryResp = sysCodeFacade.findSysCodeByType("country");
			if(countryResp.isSusscess() && CollectionUtils.isNotEmpty(countryResp.getData())){
				for(DictDTO dto : countryResp.getData()){
					countryMap.put(dto.getKey(), dto.getValue());
				}
			}
			//加载省份码字典
			ResponseData<List<DictDTO>> provinceResp = sysCodeFacade.findSysCodeByType("province");
			if(provinceResp.isSusscess() && CollectionUtils.isNotEmpty(provinceResp.getData())){
				for(DictDTO dto : provinceResp.getData()){
					provinceMap.put(dto.getKey(), dto.getValue());
				}
			}
			//加载城市字典
			ResponseData<List<DictDTO>> cityResp = sysCodeFacade.findSysCodeByType("city");
			if(cityResp.isSusscess() && CollectionUtils.isNotEmpty(cityResp.getData())){
				for(DictDTO dto : cityResp.getData()){
					cityMap.put(dto.getKey(), dto.getValue());
				}
			}
			//加载地区字典
			ResponseData<List<DictDTO>> disResp = sysCodeFacade.findSysCodeByType("district");
			if(disResp.isSusscess() && CollectionUtils.isNotEmpty(disResp.getData())){
				for(DictDTO dto : disResp.getData()){
					districtMap.put(dto.getKey(), dto.getValue());
				}
			}
			//加载银行字典
			ResponseData<List<DictDTO>> bankResp = sysCodeFacade.findSysCodeByType("bank");
			if(bankResp.isSusscess() && CollectionUtils.isNotEmpty(bankResp.getData())){
				for(DictDTO dto : bankResp.getData()){
					bankMap.put(dto.getKey(), dto.getValue());
				}
			}
			
		}catch(Exception e){
			logger.error("init sysCode into memory from db return exception, msgInfo:" + e.getMessage(), e);
		}
		logger.info("init sysCode into memory end at time:{}", DateTimeUtility.timeofnow());
	}

}
